<?php

namespace App\Http\Controllers\Home\User\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\Home\UserAccount;
use App\Http\Requests\Home\UserAddress;
use App\Model\Home_users;
use App\Http\Controllers\Common\CommonApi;
use Session;

class UserCenterApi extends Controller
{
    /**
     * [json json的返回信息]
     * @param  [type] $code [返回状态码]
     * @param  [type] $msg  [返回的信息内容]
     * @param  array  $data [返回所需数据]
     * @return [type]       [description]
     */
    public function json($code, $msg, $data = [])
    {
        return response()->json([
        'status' => $code,
        'msg' => $msg,
        'data' => $data,
        ]);
    }

    public function orders()
    {	
    	//该用户ID,确认订单是哪个用户
    	$uid = session('home_user')['id'];

    	//查询用户的订单信息
    	$userorder = DB::table('orders_details as od')
    	->select(['od.id', 'od.oid', 'od.gname', 'od.gprice_id', 'od.deal_price', 'od.buy_num', 'od.color', 'od.size', 'od.message', 'o.orders_num', 'o.orders_sum', 'o.state', 'o.created_at'])
    	->leftJoin('orders as o','o.id','=','od.oid')
    	->where('o.uid', '=', $uid)
    	->paginate(3);

    	return $userorder;
    }

    public function orders0()
    {	
    	$uid = session('home_user')['id'];
    	$userorder = DB::table('orders_details as od')
    	->select(['od.id', 'od.oid', 'od.gname', 'od.gprice_id', 'od.deal_price', 'od.buy_num', 'od.color', 'od.size', 'od.message', 'o.orders_num', 'o.orders_sum', 'o.state', 'o.created_at'])
    	->leftJoin('orders as o','o.id','=','od.oid')
    	->where('o.uid', '=', $uid)
    	->where('o.state', '=', '0')
    	->get()->toArray();

    	return $userorder;
    }

    public function orders1()
    {	
    	$uid = session('home_user')['id'];
    	$userorder = DB::table('orders_details as od')
    	->select(['od.id', 'od.oid', 'od.gname', 'od.gprice_id', 'od.deal_price', 'od.buy_num', 'od.color', 'od.size', 'od.message', 'o.orders_num', 'o.orders_sum', 'o.state', 'o.created_at'])
    	->leftJoin('orders as o','o.id','=','od.oid')
    	->where('o.uid', '=', $uid)
    	->where('o.state', '=', '1')
    	->get()->toArray();

    	return $userorder;
    }

    public function orders2()
    {	
    	$uid = session('home_user')['id'];
    	$userorder = DB::table('orders_details as od')
    	->select(['od.id', 'od.oid', 'od.gname', 'od.gprice_id', 'od.deal_price', 'od.buy_num', 'od.color', 'od.size', 'od.message', 'o.orders_num', 'o.orders_sum', 'o.state', 'o.created_at'])
    	->leftJoin('orders as o','o.id','=','od.oid')
    	->where('o.uid', '=', $uid)
    	->where('o.state', '=', '2')
    	->get()->toArray();

    	return $userorder;
    }

    public function searchOrders(Request $Request)
    {
    	//该用户ID,确认订单是哪个用户
    	$uid = session('home_user')['id'];
    	
    	$keyword = $Request->input('keyword');
    	$keyword = "%".$keyword."%";

    	//查询用户的订单信息
    	$orderlist = DB::table('orders_details as od')
    	->select(['od.id', 'od.oid', 'od.gname', 'od.gprice_id', 'od.deal_price', 'od.buy_num', 'od.color', 'od.size', 'od.message', 'o.orders_num', 'o.orders_sum', 'o.state', 'o.created_at'])
    	->leftJoin('orders as o','o.id','=','od.oid')
    	->where('o.uid', '=', $uid)
    	->where('orders_num', 'like', $keyword)
    	->where('od.gname', 'like', $keyword)
    	->paginate(3);

    	return view ('Home/User/order', ['userorder' => $orderlist]);
    }

    public function orders_details($oid)
    {	
    	$oid = $oid;

        $orderinfo = DB::table('orders')->find($oid);

        $orderdetail = DB::table('orders_details as od')
        ->select(['od.id', 'od.oid', 'od.gname', 'od.gprice_id', 'od.deal_price', 'od.buy_num', 'od.color', 'od.size', 'od.message', 'o.orders_sum', 'o.state', 'o.created_at'])
        ->leftJoin('orders as o','o.id','=','od.oid')
        ->where('oid', '=', $oid)
        ->get()
        ->toArray();

    	$userorder_detail = [$orderinfo, $orderdetail];

    	return $userorder_detail;
    }

     public function editOdState(Request $Request)
    {
        $input = $Request->all();

        $oid = $input['oid'];

        $bool=DB::update('update orders set state= ? where id = ? ',[3, $oid]);

        if ( $bool ) {
		    return  redirect('/myorder')->with('mess','添加成功');
		} else {
			return  redirect('/myorder')->with('mess','添加失败');
		}
    }

    public function editUc(Request $Request)
    {   
         $this->validate($Request, [

            'nickname'  => 'required',
            'email'     => 'required|email',
            'phone'     =>[
                'required',
                'regex:/^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$/',
            ],
            'address'   => 'required',
            'sex'       => 'required'
            ],

             [
                'nickname.required'  => '请输入昵称名',

                'email.required' => '请输入邮箱',
                'email.email'    => '邮箱不合法',

                'phone.required'   => '请输入手机号码',
                'phone.regex'      => '手机号不合法',

                'sex.required'     => '性别不能为空',

                'address.required' => '地址不能为空',
        ]);

        //该用户ID,确认订单是哪个用户
        $uid = session('home_user')['id'];

        $nickname = $Request->input('nickname');
        $email = $Request->input('email');
        $phone = $Request->input('phone');
        $address = $Request->input('address');
        $sex = $Request->input('sex');
        $pic = $Request->input('file_upload');

        $userdata = Home_users::where('id', $uid)->update([
            'nickname' => $nickname,
            'email' => $email,
            'phone' => $phone,
            'address' => $address,
            'sex' => $sex,
        ]);

        if ($userdata) {

            $user = Home_users::select('username', 'id', 'nickname', 'sex', 'phone', 'email', 'user_level_id', 'user_score', 'address')
            ->where('id', $uid)
                ->first();
            
            $Request->session()->put('home_user', $user);


            return $this->json(1200, '保存成功');
        }
            return $this->json(1400, '保存失败,请重试');
    }

    public function showaddress()
    {   
        $uid = session('home_user')['id'];

        $useraddress = DB::table('address')->select('id', 'link_man', 'phone', 'address', 'code', 'state')
        ->where('uid', '=', $uid)
        ->get()->toArray();

        return $useraddress;
    }

    public function doAddAddress(Request $Request)
    {   
        $this->validate($Request, [

            'link_man'  => 'required',
            'code'      => 'required',
            'phone'     =>[
                'required',
                'regex:/^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$/',
            ],
            'address'   => 'required',
            ],

            [

            'link_man.required'  => '请输入收货人',

            'phone.required'   => '请输入手机号码',
            'phone.regex'      => '手机号不合法',

            'code.required'     => '邮编不能为空',

            'address.required' => '地址不能为空',

        ]);

        $uid = session('home_user')['id'];
        $linkman = $Request->input('link_man');
        $address = $Request->input('address');
        $phone = $Request->input('phone');
        $code = $Request->input('code');
        $state = $Request->input('state');

        if (empty($state)){
            $state = 0;
        }

        $userAd =  DB::insert("insert into address (link_man, address, phone, code, state, uid) values (?, ?, ?, ?, ?, ?)", [$linkman, $address, $phone, $code, $state, $uid]);

        if($userAd){
            echo '添加成功';

        } else {
            echo '添加失败,请重试';
        } 
    }

    public function doeditAd(Request $Request)
    {   
        $this->validate($Request, [

            'link_man'  => 'required',
            'code'      => 'required',
            'phone'     =>[
                'required',
                'regex:/^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$/',
            ],
            'address'   => 'required',

            ],

            [

            'link_man.required'  => '请输入收货人',

            'phone.required'   => '请输入手机号码',
            'phone.regex'      => '手机号不合法',

            'code.required'     => '邮编不能为空',

            'address.required' => '地址不能为空',

        ]);

        $aid = $Request->input('aid');
        $linkman = $Request->input('link_man');
        $phone = $Request->input('phone');
        $address = $Request->input('address');
        $code = $Request->input('code');
        $state = $Request->input('state');

        if (empty($state)){
            $state = 0;
        }

        $bool = DB::update('update address set link_man= ?, state= ?, phone= ?, code= ?, address= ? where id = ? ', [$linkman, $state, $phone, $code, $address, $aid]);

        if ( $bool ) {
            return  redirect('/address')->with('mess','添加成功');
        } else {
            return  redirect('/address')->with('mess','添加失败');
        }
    }

    public function dodeleteAd($id)
    {
        $bool = DB::delete('delete from address where id= ?', [$id]);

        if ( $bool ) {
            return  redirect('/address')->with('mess','删除成功');
        } else {
            return  redirect('/address')->with('mess','删除失败失败');
        }

    }

    public function dolike($gid)
    {
        $uid = session('home_user')['id'];
        
        $fagoods = DB::table('goods')->find($gid);

        $userFa =  DB::insert("insert into collect (uid, gid) values (?, ?)", [$uid, $gid]);
       
        return  back();

    }

    public function showFagoods()
    {
        $uid = session('home_user')['id'];

        $a = DB::table('collect')->where('uid', '=', $uid)->get()->toArray();

        foreach ($a as $v){

            $gid = $v->gid;

            $userFaGoods[] = DB::table('goods')->where('id', '=', $gid)->get()->toArray();
        }

        if(!isset($userFaGoods)){
            return [];
        }

        return $userFaGoods;
    }

    public function doRmlick($gid)
    {
        DB::delete('delete from collect where gid= ?', [$gid]);

        $bool=DB::update('update goods set attr1= ? where id = ? ',[0, $gid]);

        if ($bool) {
            return  redirect('/favourites')->with('mess','删除成功');
        } else {
            return  redirect('/favourites')->with('mess','删除失败');
        }
    }
}
